#-*- coding: utf-8 -*-
import pygtk
pygtk.require('2.0')
import gtk
import pango
import usagi.tango
from usagi.window import ScrollMess

class RenshuuWindow(gtk.Window):
    def __init__(self,app,cur):
        gtk.Window.__init__(self)
        self.app = app
        self.cur=cur
        self.connect("delete_event", self.delete_event)
        self.set_title(u"兎先生::練習")
        self.yomikatastyle =  pango.FontDescription ("normal 15")

        self.vbox= gtk.VBox(False, 0)
        self.mhbox=gtk.HBox(False, 0)
        self.commentvbox=gtk.VBox(False,0)
        self.mhbox.pack_start(self.vbox)
        self.mhbox.pack_end(self.commentvbox)
        self.mhbox.show()
        self.add(self.mhbox)

        # A question
        self.doyou = gtk.Label("Do you know this word?")
        self.vbox.pack_start(self.doyou)

        # The word
        self.wordlab = gtk.Label()
        jap_font = pango.FontDescription ("normal 30")
        self.wordlab.modify_font (jap_font)
        self.vbox.pack_start(self.wordlab)

        # Show ansers button
        self.showanswers = gtk.Button(None,gtk.STOCK_DIALOG_QUESTION)
        self.showanswers.get_children()[0].get_children()[0].get_children()[1].set_text(u"答え")
        self.showanswers.connect("clicked", self.show_answer)
        self.vbox.pack_start(self.showanswers)

        # The answers
        self.answerframe=gtk.Frame(u"The answer")
        self.answerbox = gtk.VBox(False, 0)
        self.answerframe.add(self.answerbox)
        self.vbox.pack_start(self.answerframe)

        # Examples from the tanaka corpus
        self.tanaka = gtk.Button(None, gtk.STOCK_INDEX)
        self.tanaka.connect("clicked", self.showtanaka)
        self.tanaka.get_children()[0].get_children()[0].get_children()[1].set_text(u"例")
        self.vbox.pack_start(self.tanaka,False)

        # Mastered?
        self.didyou = gtk.Label("\n\nDid you know it? (Don't lie, there's nobody watching.)")
        self.vbox.pack_start(self.didyou,False)

        # Yes|No
        self.hbox = gtk.HBox(False,0)
        yes = gtk.Button(None,gtk.STOCK_YES)
        yes.connect("clicked", self.theansweris, "yes")
        self.hbox.pack_start(yes)
        no  = gtk.Button(None,gtk.STOCK_NO)
        no.connect("clicked", self.theansweris, "no")
        self.hbox.pack_start(no)
        self.vbox.pack_end(self.hbox,False)

        # Comments
        self.comlab = gtk.Label("You can comment on this word\n for later reference.")
        self.commentvbox.pack_start(self.comlab,False)
        self.comview= gtk.TextView()
        self.combuf = self.comview.get_buffer()
        self.commentvbox.pack_start(self.comview)
        self.savebtn= gtk.Button(None, gtk.STOCK_SAVE)
        self.savebtn.connect("clicked", self.i_comment)
        self.commentvbox.pack_end(self.savebtn,False)

    def showtanaka(self,w):
        usagi.window.ScrollMess("Examples",self,\
        "These phrases contain the word %s:" %self.word.kanji, \
        self.word.get_corpus()).showanddie()

    def theansweris(self,w,d):
        if d=='yes':
            self.cur.mark_word_mastered(self.word.kanji)
        else:
            self.cur.mark_word_mastered(self.word.kanji,0)
        self.set_word(self.cur.fetch_word())

    def i_comment(self,w):
        start, end = self.combuf.get_bounds()
        self.cur.set_word_comment(self.word.kanji,self.combuf.get_text(start,end))

    def show_answer(self,w=None):
        self.showanswers.hide()
        self.doyou.hide()
        self.tanaka.show()
        self.didyou.show()
        self.answerframe.show_all()
        self.hbox.show_all()
        self.commentvbox.show_all()

    
    def set_word(self, word):
        if word==None:
            self.remove(self.mhbox)
            self.add(gtk.Label("You've practiced enough on those kanjis for today.\n Add more kanjis or go rest."))
            self.show_all()
        else:
            self.wordlab.set_text(word[0])
            self.combuf.set_text(word[1])
            self.hbox.hide()
            self.tanaka.hide()
            self.didyou.hide()
            self.answerframe.hide()
            self.commentvbox.hide_all()
            self.doyou.show()
            self.showanswers.show()
            self.wordlab.show()
            self.vbox.show()
            self.show()
            while gtk.events_pending():
                gtk.main_iteration()
            self.word = usagi.tango.Word(word[0])
            self._set_answers(self.word)
    
      
    def _set_answers(self,w):
        self.answerbox.foreach(self.answerbox.remove)
        for i in w.interpretations:
            label = gtk.Label(i[0])
            label.modify_font(self.yomikatastyle)
            self.answerbox.pack_start(label)
            label = gtk.Label("[%s]"%"|".join(i[1]))
            self.answerbox.pack_start(label)
            for j in i[2]:
                label = gtk.Label(j)
                self.answerbox.pack_start(label)

    def delete_event(self,w,e=None,d=None):
        self.app.back_from_practice()
        return False
        
